<?php
namespace Manage\Controller;

use Admin\Model\SpecialViewModel;
use Think\Model;
use Home\Event\JssdkEvent;

class SpecialController extends BaseController{

    public function index() {
        $p = max(I('param.p'), 1);
        $num = max(I('param.num'), 20);
        $this->assign('page', $p);
        $this->assign('num', $num);

        $order_field = I('param.order_field', 'id');
        $order = I('param.order', 'desc');
        $sunxu = $order == 'asc' ? 'desc' : 'asc';
        $this->assign('order_field', $order_field);
        $this->assign('order', $sunxu);
        $order = array($order_field => $order);

        $search_field = I('param.search_field');
        $search = trim(I('param.search'));
        $where[0] = ' 1=1 ';
        if ($search && $search_field) {
            $where[] = $search_field." like '%".$search."%'";
            $this->assign('search',$search);
            $this->assign('search_field',$search_field);
        }
        $count = M('b2c_special_template')->where($where)->count();
        $Page = new \Think\Page($count, $num);
        $show = $Page->show();
        $fields = 'id,title,title2,title3,create_time';
        $data = M('b2c_special_template')
            ->field($fields)
            ->page($p, $num)
            ->where($where)
            ->order($order)
            ->select();
        $this->assign('list',$data);
        $this->assign('page',$show);
        $this->display();
    }

    public function add() {
        $this->display('edit');
    }

    public function edit() {
        if (IS_POST) {
            $data['id'] = I('post.id');
            $data['title'] = I('post.title');
            $data['title2'] = I('post.title2');
            $data['title3'] = I('post.title3');
            $data['title4'] = I('post.title4');
            $data['title5'] = I('post.title5');
            $data['img_url'] = I('post.img_url');
            $data['ico_url'] = I('post.ico_url');
            $data['type'] = I('post.type');
            $data['update_time'] = time();
            if ($data['id']) {
                $res = M('b2c_special_template')->save($data);
            } else {
                unset($data['id']);
                $data['create_time'] = time();
                $res = M('b2c_special_template')->add($data);
            }
            if ($res !== false) {
                $this->success('成功!',U('special/index'));
            } else {
                $this->error('失败!');
            }
        } else {
            $id = I('get.id');
            $info = M('b2c_special_template')->where(array('id'=>$id))->find();
            $this->assign('info', $info);
            $this->display();
        }
    }

    public function deleteImg() {
        $id = I('get.id');
        $field = I('get.type');
        if ($id && $field) {
            $res = M(b2c_special_template)->where(array('id'=>$id))->save(array($field=>''));
        } else {
            $res = false;
        }
        if ($res !== false) {

            $this->ajaxReturn(array('status'=>true, 'message'=>'操作成功'));
        } else {
            $this->ajaxReturn(array('status'=>false, 'message'=>'操作失败'));

        }
    }

    //批量删除
    public function del() {
        if (IS_AJAX) {
            $id = I('param.id');
            if ($id) {
                if (is_array($id)) {
                    $id = join(',' ,$id);
                    $del = $id;
                    $res = M('b2c_special_template')->delete($del);

                } else {
                    $del = array('id'=>$id);
                    $res = M('b2c_special_template')->where($del)->delete();
                }
                if ($res !== false) {
                    $return = array(
                        'status'=>true,
                        'message'=>'删除成功',
                    );
                } else {
                    $return = array(
                        'status'=>false,
                        'message'=>'操作失败',
                    );
                }
            } else {
                $return = array(
                    'status'=>false,
                    'message'=>'供应商ID不能为空',
                );
            }
            $this->ajaxReturn($return);
        }
        $return = array(
            'status'=>false,
            'message'=>'非法访问',
        );
        $this->ajaxReturn($return);
    }

    /**
     * 专题模板预览
     */
    public function viewTemplate() {
        $template_id = I('param.template_id');
        if (!$template_id) {
            $this->error('缺少参数');
        }
        //后台预览不需要走微信的分享授权
        /*$jssdk = new JssdkEvent();
        $signPackage = $jssdk->GetSignPackage();
        $this->assign('signPackage', $signPackage);*/

        $specialModel = new SpecialViewModel();
        $specialModel->template_id = $template_id;
        $data = $specialModel->viewTemplate();
        $this->assign($data);
        $this->display('Admin@Special:viewTemplate');
    }
}